﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstrumentCheck.Module;
using InstrumentCheck.BLL;

namespace InstrumentCheck.WebAppUI
{
    public partial class uploadImage : BasePage
    {
        List<Attach> listImage = new List<Attach>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FormRouting routing = Session["Routing"] as FormRouting;
                int FlowNO = 10;
                if (routing != null)
                    FlowNO = routing.FlowNO;

                switch (FlowNO)
                {
                    case 0:
                    case 30:
                        pnlUpload.Visible = true;
                        break;
                    default:
                        pnlUpload.Visible = false;
                        break;
                }

                if (Session["AttachList"] != null)
                    listImage = Session["AttachList"] as List<Attach>;

                if (Session["Form"] != null)
                {
                    ApplyForm form = Session["Form"] as ApplyForm;
                    listImage = form.Item.ItemAttachment;
                }
                ViewState["listAll"] = listImage;
                listImage = listImage.Where(a => a.Type == AttachmentType.Picture).ToList<Attach>();

                ViewState["listImage"] = listImage;
                BindGridViewAttach(listImage);
            }
        }        

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (!upload.HasFile)
                return;

            string strFileOriginalName = upload.FileName;  //原始文件名
            string strNameAll = upload.PostedFile.FileName;  //带路径的原始文件名
            string type = strFileOriginalName.Substring(strFileOriginalName.LastIndexOf('.') + 1);

            try
            {
                string strServerName = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;
                upload.SaveAs(PublicFunction.strAttachPath + "\\" + strServerName);

                Attach attach = new Attach();
                attach.ClientName = strFileOriginalName;
                attach.SystemName = strServerName;
                attach.Type = AttachmentType.Picture;
                List<Attach> list = ViewState["listImage"] as List<Attach>;
                list.Add(attach);
                BindGridViewAttach(list);
            }
            catch
            {
                throw;
            }
        }

        private void BindGridViewAttach(List<Attach> listAttach)
        {
            gvImage.DataSource = listAttach;
            gvImage.DataBind();

            if (listAttach == null || listAttach.Count == 0)
                gvImage.Visible = false;
            else
                gvImage.Visible = true;
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            List<Attach> lAll = ViewState["listAll"] as List<Attach>;
            lAll = lAll.Where(a => a.Type == AttachmentType.Attachment).ToList<Attach>();

            //需要把上传的信息返回到父页面，并关闭当前页面
            string strAttachURL = "", strAttachInfo = "";
            List<Attach> list = ViewState["listImage"] as List<Attach>;
            for (int i = 0; i < list.Count; i++)
            {
                strAttachURL += "<a target=\"_blank\" href=\"" + AttachURL() + list[i].SystemName + "\">" + list[i].ClientName + "</a>;";
                strAttachInfo += list[i].SystemName + "~";
                strAttachInfo += list[i].ClientName + ";";
                lAll.Add(list[i]);
            }
            if (strAttachInfo != string.Empty)
                strAttachInfo = strAttachInfo.Substring(0, strAttachInfo.Length - 1);

            if (Session["AttachList"] != null)
                Session["AttachList"] = lAll;
            else
            {
                ApplyForm form = Session["Form"] as ApplyForm;
                form.Item.ItemAttachment = lAll;
                Session["Form"] = form;
            }         

            Page.ClientScript.RegisterStartupScript(this.GetType(), "Image", "Image('" + strAttachURL + "','" + strAttachInfo + "')", true);
        }
       
        protected void gvImage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[1].Visible = false;
                FormRouting routing = Session["Routing"] as FormRouting;
                int FlowNO = routing.FlowNO;

                if (FlowNO == 0 || FlowNO == 30)
                    e.Row.Cells[3].Visible = true;
                else
                    e.Row.Cells[3].Visible = false;
            }

            //构造链接
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[2].Text = "<a target='_blank' href='" + AttachURL() + e.Row.Cells[1].Text + "'>" + e.Row.Cells[0].Text + "</a>";
            }
        }

        protected void gvImage_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<Attach> list = ViewState["listImage"] as List<Attach>;
            string strSystemName = gvImage.Rows[e.RowIndex].Cells[1].Text.Trim();

            Attach attach = (from a in list
                             where a.SystemName == strSystemName
                             select a).FirstOrDefault();
            list.Remove(attach);
            ViewState["listImage"] = list;
            BindGridViewAttach(list);            
        }
    }
}
